<?php
include "otamu_config.php";

switch ($_REQUEST['act']) {

	case "get_cawangan_info" :
		$find = $_REQUEST['cawangan_id'];
		$sql = "SELECT * FROM cawangan WHERE id_cawangan='$find'";
		$res = pg_query($sql);
		if (pg_num_rows($res) == 1) {
			$cawangan = pg_fetch_object($res);
			echo "$cawangan->alamat1--%%--$cawangan->alamat2--%%--$cawangan->poskod--%%--$cawangan->bandar";
		} else {
			echo "0";
		}
		break;
	
	case "get_student" :
		$find = $_REQUEST['find_nomatrik'];
		$sql = "SELECT nomatrik,ic_new,name,code,room_no,course_code,semester FROM vw_pelajar WHERE ic_new='$find' OR nomatrik='$find' OR em_cardno='$find'";
		$res = pg_query($sql);
		if (pg_num_rows($res) == 1) {
			$student = pg_fetch_object($res);
			echo "$student->nomatrik,$student->ic_new,$student->name,$student->room_no,$student->course_code$student->semester";
		} else {
			echo "0";
		}
		break;
	
	case "daerahs_of_negeri" :
		$negeri_id = $_REQUEST['negeri_id'];
		$sql = "SELECT * FROM ref_daerah WHERE negeri_id='$negeri_id' ORDER BY daerah";
		$res = pg_query($sql);
		if (pg_num_rows($res) > 0) {
			while ($dat = pg_fetch_object($res)) {
				$arr[] = $dat->daerah."=".$dat->id;
			}
			echo join("$$",$arr);	
		} else {
			echo "0";
		}
		break;

	case "daerahs_of_negeri_old" :
		$negeri_id = $_REQUEST['negeri_id'];
		$sql = "SELECT * FROM ref_daerah WHERE negeri_id='$negeri_id' ORDER BY daerah";
		$res = pg_query($sql);
		if (pg_num_rows($res) > 0) {
			while ($dat = pg_fetch_object($res)) {
				$arr[] = $dat->daerah."=".$dat->kod_daerah;
			}
			echo join("$$",$arr);	
		} else {
			echo "0";
		}
		break;

	case "parlimens_of_negeri" :
		$negeri_id = $_REQUEST['negeri_id'];
		$sql = "SELECT * FROM ref_parlimen WHERE negeri_id='$negeri_id' ORDER BY parlimen";
		$res = pg_query($sql);
		if (pg_num_rows($res) > 0) {
			while ($dat = pg_fetch_object($res)) {
				$arr[] = $dat->parlimen."=".$dat->id;
			}
			echo join("$$",$arr);	
		} else {
			echo "0";
		}
		break;

	case "duns_of_parlimen" :
		$parlimen_id = $_REQUEST['parlimen_id'];
		$sql = "SELECT * FROM ref_dun WHERE parlimen_id='$parlimen_id' ORDER BY dun";
		$res = pg_query($sql);
		if (pg_num_rows($res) > 0) {
			while ($dat = pg_fetch_object($res)) {
				$arr[] = $dat->dun."=".$dat->id;
			}
			echo join("$$",$arr);	
		} else {
			echo "0";
		}
		break;

	case "subs_of_item" :
		$id = $_REQUEST['id'];
		$sql = "SELECT * FROM sub_items WHERE item_id='$id' ORDER BY name";
		$res = pg_query($sql);
		if (pg_num_rows($res) > 0) {
			while ($dat = pg_fetch_object($res)) {
				$arr[] = $dat->name."=".$dat->id;
			}
			echo join("$$",$arr);	
		} else {
			echo "0";
		}
		break;

	case "external_list" :
		$table_name_id = $_REQUEST['t_name_id'];
		//get table name
		$sql = "SELECT table_name FROM object_categories WHERE id=$table_name_id";
		$res = pg_query($sql);
		$dat = pg_fetch_object($res,0);
		$table_name = $dat->table_name;
		
		$sql = "SELECT * FROM $table_name ORDER BY name";
		$res = pg_query($sql);
		if (pg_num_rows($res) > 0) {
			while ($dat = pg_fetch_object($res)) {
				$arr[] = $dat->name."=".$dat->id;
			}
			echo join("$$",$arr);	
		} else {
			echo "0";
		}
		break;

	case "check_exist_data" :
		$sql = "SELECT * FROM statistics WHERE 
			         sub_item_id='$sub_item_id' AND 
					 agency_id='$agency_id' AND
					 state_id='$state_id' AND
					 date_part('year',the_date)='$the_year'";
		$res = pg_query($sql);
		if (pg_num_rows($res) > 0) {
			echo "1";
		}
		else {
			echo "0";
		}

	
	case "get_sub_item_name" :
		$external_table_id = $_REQUEST['external_table_id'];
		$id = $_REQUEST['external_table_id'];
		//get table name
		$sql = "SELECT table_name FROM object_categories WHERE id='$external_table_id'";
		$res = pg_query($sql);
		$dat = pg_fetch_object($res,0);
		$table_name = $dat->table_name;

		$id = $_REQUEST['id'];
		$sql = "SELECT * FROM $table_name WHERE id='$id' ORDER BY name";
		$res = pg_query($sql);
		if (pg_num_rows($res) > 0) {
			$dat = pg_fetch_object($res,0);
			echo $dat->name;
		}
		break;
	
	default :
		echo "1";
		break;
}
?>
